<?php


namespace app\api\controller;


use app\common\controller\ApiController;
use think\Exception;

class Mortgage extends ApiController {

    /**
     * 是否签名验证
     * @var bool
     */
    protected $isSign = false;

    /**
     * 是否加密解密
     * @var bool
     */
    protected $isEncrypt = false;

    protected $headersFields = [];
    protected $notNeedToken = ['*'];

    protected $isArrayResult = ['getlist'];

    /**
     * 房贷计算
     * @return false|string
     */
    public function mortgageCount() {
        try {

            $fields = [
                'min',
                'max',
                'ratio',
                'money',
                'stage',
                'type',
                'lpr',
                'base',
                'rate',
                'is_taxation',
                'guarantee',
                'deed_tax',
                'person_tax',
                'notarial_fees',
                'property_fee'
            ];
            $params = $this->checkParams($fields);
            if ($params === false) {
                return $this->resErr();
            }

            if (!$id = \loan\Mortgage::mortgageCount($params)) {
                return $this->resErr();
            }

            return $this->resData('success', ['id' => $id]);
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 计算后的列表
     * @return false|string
     * @throws Exception
     */
    public function getList() {
        $params = $this->checkParams('id');
        if ($params === false) {
            return $this->resErr();
        }

        if (!$data = \loan\Mortgage::getList($params['id'])) {
            return $this->resErr();
        }

        return $this->resData('success', $data);
    }

    /**
     * 详情
     * @return false|string
     * @throws Exception
     */
    public function getInfo() {
        $params = $this->checkParams('id');
        if ($params === false) {
            return $this->resErr();
        }

        if (!$data = \loan\Mortgage::getInfo($params['id'])) {
            return $this->resErr();
        }

        return $this->resData('success', $data);
    }

    /**
     * 比例配置项
     * @return false|string
     */
    public function getConfig() {

        return $this->resData('success', [
            'first_ratio' => config('mortgage.first_ratio'),
            'business_ratio' => config('mortgage.business_ratio'),
        ]);

    }
}
